/*
 * @lc app=leetcode.cn id=49 lang=typescript
 *
 * [49] 字母异位词分组
 */

// @lc code=start
interface Alphabet {
    [key: string]: any,
}

function groupAnagrams(strs: string[]): string[][] {
    let obj: Alphabet = {
        'a': 2, 'b': 3, 'c': 5, 'd': 7, 'e': 11, 'f': 13, 'g': 17, 'h': 19, 'i': 23, 'j': 29, 'k': 31, 'l': 37, 'm': 41,
        'n': 43, 'o': 47, 'p': 53, 'q': 59, 'r': 61, 's': 67, 't': 71, 'u': 73, 'v': 79, 'w': 83, 'x': 89, 'y': 97, 'z': 101
    }
    const map = new Map();
    const res: any = [];
    for (let i = 0; i < strs.length; i++) {
        let sum = 1;
        const v = strs[i];
        for (let j = 0; j < v.length; j++) {
            sum = sum * obj[v.charAt(j)];
        }
        const index = map.get(sum);
        if (index !== undefined) {
            res[index].push(v);
        } else {
            const a = new Array(v)
            res.push(a);
            map.set(sum, map.size);
        }
    }
    // console.log(res);
    return res;
};

// groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]);
// @lc code=end

